package com.yq.cp.config.mqtt;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.yq.cp.config.LogPrintAspect;
import com.yq.cp.entity.data.NengKongData;
import com.yq.cp.utils.DateTimeUtil;
import com.yq.cp.utils.TimerUtil;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 常规MQTT回调函数
 *
 * @author Mr.Qu
 * @since 2020/1/9 16:26
 */
public class Callback implements MqttCallback {
    private static final Logger logger = LoggerFactory.getLogger(LogPrintAspect.class);


    private String[] topics={"nktopic"};
    private String[] energyCodes={"sgr001"};

    private BigDecimal glmrz1=new BigDecimal(0);
    private BigDecimal glmrz2=new BigDecimal(0);


    /**
     * MQTT 断开连接会执行此方法
     */
    @Override
    public void connectionLost(Throwable throwable) {
        logger.info("断开了MQTT连接 ：{}", throwable.getMessage());
        logger.error(throwable.getMessage(), throwable);
    }

    /**
     * publish发布成功后会执行到这里
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        logger.info("发布消息成功");
    }

    /**
     * subscribe订阅后得到的消息会执行到这里
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        //  TODO    此处可以将订阅得到的消息进行业务处理、数据存储
        logger.info("收到来自 " + topic + " 的消息：{}", message.toString());
        if (topic.equals("Chongqing;Saigeer")){

            JSONObject jsonObject = JSON.parseObject(message.toString());
            String energyCode="Saigeer";

            JSONObject jsonObject1 = jsonObject.getJSONObject("Inverter;001;Solis;GCI-(30-40)K-5G");
            JSONObject jsonObject2  = jsonObject.getJSONObject("Inverter;002;Solis;GCI-(30-40)K-5G");
            JSONObject jsonObject3  = jsonObject.getJSONObject("Inverter;003;Solis;GCI-(30-40)K-5G");
            JSONObject jsonObject4  = jsonObject.getJSONObject("Meteorograph;001;建大仁科;XXX-XX");
            JSONObject jsonObject51  = jsonObject.getJSONObject("ElectricityMeter;130001241059;GENERIC;XXX-XX");//上网电量
            JSONObject jsonObject52  = jsonObject.getJSONObject("ElectricityMeter;130017140265;GENERIC;XXX-XX");//下网电量
            JSONObject jsonObject53  = jsonObject.getJSONObject("ElectricityMeter;130001238820;GENERIC;XXX-XX");//上网电量
            //功率
            String name1="ModBus.逆变器";
            String name2=".有功功率W";
//            this.add(energyCode,"001",name1+"1"+name2,new BigDecimal(jsonObject1.getDouble("有功功率")));
//            this.add(energyCode,"002",name1+"2"+name2,new BigDecimal(jsonObject2.getDouble("有功功率")));
//            this.add(energyCode,"003",name1+"3"+name2,new BigDecimal(jsonObject3.getDouble("有功功率")));

            //功率
            //直流功率
//            name1="ModBus.逆变器";
//            name2=".直流功率W";
//            this.add(energyCode,"001",name1+"1"+name2,new BigDecimal(jsonObject1.getDouble("总直流输入功率")));
//            this.add(energyCode,"002",name1+"2"+name2,new BigDecimal(jsonObject2.getDouble("总直流输入功率")));
//            this.add(energyCode,"003",name1+"3"+name2,new BigDecimal(jsonObject3.getDouble("总直流输入功率")));
            //直流功率

            //发电量
            name1="ModBus.逆变器";
            name2=".当日发电量01kWh";

//            this.add(energyCode,"001",name1+"1"+name2,new BigDecimal(jsonObject1.getDouble("电表总有功发电量")));
//            this.add(energyCode,"002",name1+"2"+name2,new BigDecimal(jsonObject2.getDouble("电表总有功发电量")));
//            this.add(energyCode,"003",name1+"3"+name2,new BigDecimal(jsonObject3.getDouble("电表总有功发电量")));
//            this.add(energyCode,null,name1+name2,new BigDecimal(jsonObject1.getDouble("电表总有功发电量"))
//                    .add(new BigDecimal(jsonObject2.getDouble("电表总有功发电量"))).add(new BigDecimal(jsonObject3 .getDouble("电表总有功发电量"))));

            //发电量

            //温度
//            name1="ModBus.室外温湿度传感器";
//            name2=".温度";
//            this.add(energyCode,null,name1+name2,new BigDecimal(jsonObject4.getDouble("室外温度")));
//            name2=".湿度";
//            this.add(energyCode,null,name1+name2,new BigDecimal(jsonObject4.getDouble("室外湿度")));

            //温度

            //电量
//                    "下网电量总": 1681,
//                    "下网电量尖": 168,
//                    "下网电量峰": 168,
//                    "下网电量平": 168,
//                    "下网电量谷": 168,
//                    "上网电量总": 16888,
//                    "上网电量尖": 169,
//                    "上网电量峰": 169,
//                    "上网电量平": 169,
//                    "上网电量谷": 169
//            JSONObject jsonObject51  = jsonObject.getJSONObject("ElectricityMeter;130001241059;GENERIC;XXX-XX");//下网电量
//            JSONObject jsonObject52  = jsonObject.getJSONObject("ElectricityMeter;130017140265;GENERIC;XXX-XX");//上网电量
//            JSONObject jsonObject53  = jsonObject.getJSONObject("ElectricityMeter;130001238820;GENERIC;XXX-XX");//上网电量
            //265  800
            //059  40
            //820  80

            BigDecimal a1=new BigDecimal(40);
            BigDecimal a2=new BigDecimal(80);
            BigDecimal a3=new BigDecimal(800);
            BigDecimal x1 = new BigDecimal(jsonObject51.getDouble("上网电量尖")).multiply(a1); //059
            BigDecimal x2 = new BigDecimal(jsonObject51.getDouble("上网电量峰")).multiply(a1);
            BigDecimal x3 = new BigDecimal(jsonObject51.getDouble("上网电量平")).multiply(a1);
            BigDecimal x4 = new BigDecimal(jsonObject51.getDouble("上网电量谷")).multiply(a1);
            BigDecimal x5 = new BigDecimal(jsonObject51.getDouble("上网电量总")).multiply(a1);


            BigDecimal s1 = new BigDecimal(jsonObject52.getDouble("下网电量尖")).multiply(a3);//265
            BigDecimal s2 = new BigDecimal(jsonObject52.getDouble("下网电量峰")).multiply(a3);
            BigDecimal s3 = new BigDecimal(jsonObject52.getDouble("下网电量平")).multiply(a3);
            BigDecimal s4 = new BigDecimal(jsonObject52.getDouble("下网电量谷")).multiply(a3);
            BigDecimal s5 = new BigDecimal(jsonObject52.getDouble("下网电量总")).multiply(a3);

            BigDecimal xw1 = new BigDecimal(jsonObject52.getDouble("上网电量尖")).multiply(a3);//265
            BigDecimal xw2 = new BigDecimal(jsonObject52.getDouble("上网电量峰")).multiply(a3);
            BigDecimal xw3 = new BigDecimal(jsonObject52.getDouble("上网电量平")).multiply(a3);
            BigDecimal xw4 = new BigDecimal(jsonObject52.getDouble("上网电量谷")).multiply(a3);
            BigDecimal xw5 = new BigDecimal(jsonObject52.getDouble("上网电量总")).multiply(a3);

            BigDecimal d1 = new BigDecimal(jsonObject53.getDouble("上网电量尖")).multiply(a2);//820
            BigDecimal d2 = new BigDecimal(jsonObject53.getDouble("上网电量峰")).multiply(a2);
            BigDecimal d3 = new BigDecimal(jsonObject53.getDouble("上网电量平")).multiply(a2);
            BigDecimal d4 = new BigDecimal(jsonObject53.getDouble("上网电量谷")).multiply(a2);
            BigDecimal d5 = new BigDecimal(jsonObject53.getDouble("上网电量总")).multiply(a2);



            if (x1.compareTo(new BigDecimal(0))<1){
                return;
            }
            if (x2.compareTo(new BigDecimal(0))<1){
                return;
            }
            if (x3.compareTo(new BigDecimal(0))<1){
                return;
            }
            if (x4.compareTo(new BigDecimal(0))<1){
                return;
            }
            if (x5.compareTo(new BigDecimal(0))<1){
                return;
            }

            if (s1.compareTo(new BigDecimal(0))<1){
                return;
            }
            if (s2.compareTo(new BigDecimal(0))<1){
                return;
            }
            if (s3.compareTo(new BigDecimal(0))<1){
                return;
            }
            if (s4.compareTo(new BigDecimal(0))<1){
                return;
            }
            if (s5.compareTo(new BigDecimal(0))<1){
                return;
            }


            if (d1.compareTo(new BigDecimal(0))<1){
                return;
            }
            if (d2.compareTo(new BigDecimal(0))<1){
                return;
            }
            if (d3.compareTo(new BigDecimal(0))<1){
                return;
            }
            if (d4.compareTo(new BigDecimal(0))<1){
                return;
            }
            if (d5.compareTo(new BigDecimal(0))<1){
                return;
            }



            this.add(energyCode,"001","下网电量尖",s1);
            this.add(energyCode,"001","下网电量峰",s2);
            this.add(energyCode,"001","下网电量平",s3);
            this.add(energyCode,"001","下网电量谷",s4);
            this.add(energyCode,"001","下网电量总",s5);
            this.add(energyCode,"001","上网电量尖",x1.add(d1));
            this.add(energyCode,"001","上网电量峰",x2.add(d2));
            this.add(energyCode,"001","上网电量平",x3.add(d3));
            this.add(energyCode,"001","上网电量谷",x4.add(d4));
            this.add(energyCode,"001","上网电量总",x5.add(d5));


            this.add(energyCode,"001","消网电量尖",x1.add(d1).subtract(xw1));
            this.add(energyCode,"001","消网电量峰",x2.add(d2).subtract(xw2));
            this.add(energyCode,"001","消网电量平",x3.add(d3).subtract(xw3));
            this.add(energyCode,"001","消网电量谷",x4.add(d4).subtract(xw4));
            this.add(energyCode,"001","消网电量总",x5.add(d5).subtract(xw5));




            //发电量
//            name1="ModBus.逆变器";
//            name2=".当日发电量01kWh";
//            this.add(energyCode,null,name1+name2,x5.add(d5));
            //
        }

//        for (int i = 0; i < topics.length; i++) {
//            String t = topics[i];
//            if (t.equals(topic)){
//                String energyCode = energyCodes[i];
//                sgr(message,energyCode);
//            }
//        }
    }

    //sgr
    private void sgr(MqttMessage message,String energyCode){
        JSONObject jsonObject = JSON.parseObject(message.toString());
        //功率
        String name1="ModBus.逆变器";
        String name2=".有功功率W";
        String gl1 = jsonObject.get("1p").toString();
        String gl2 = jsonObject.get("2p").toString();
        String gl3 = jsonObject.get("3p").toString();
        BigDecimal gl=new BigDecimal(gl1);
        if(gl.multiply(new BigDecimal(10)).compareTo(new BigDecimal(gl2))==-1 || gl.multiply(new BigDecimal(10)).compareTo(new BigDecimal(gl3))==-1){
            return;
        }
        gl= pdsj1(gl);
        this.add(energyCode,"B1N02-D01(B栋)",name1+"1"+name2,gl);

        gl=new BigDecimal(gl2);
        gl= pdsj1(gl);
        this.add(energyCode,"B1N02-D01(A栋)",name1+"2"+name2,gl);

        gl=new BigDecimal(gl3);
        gl= pdsj1(gl);
        this.add(energyCode,"B1N01-D01(A栋)新",name1+"3"+name2,gl);
//        this.add(energyCode,"B1N01-D01(A栋)新",name1+name2,new BigDecimal(gl1).add(new BigDecimal(gl2)).add(new BigDecimal(gl3)));
        //功率

        //直流功率
        name1="ModBus.逆变器";
        name2=".直流功率W";
        gl1 = jsonObject.get("1DCp").toString();
        gl2 = jsonObject.get("2DCp").toString();
        gl3 = jsonObject.get("3DCp").toString();
        gl=new BigDecimal(gl1);
        gl= pdsj1(gl);
        this.add(energyCode,"B1N02-D01(B栋)",name1+"1"+name2,gl);
        gl=new BigDecimal(gl2);
        gl= pdsj1(gl);
        this.add(energyCode,"B1N02-D01(A栋)",name1+"2"+name2,gl);
        gl=new BigDecimal(gl3);
        gl= pdsj1(gl);
        this.add(energyCode,"B1N01-D01(A栋)新",name1+"3"+name2,gl);
//        this.add(energyCode,"B1N01-D01(A栋)新",name1+name2,new BigDecimal(gl1).add(new BigDecimal(gl2)).add(new BigDecimal(gl3)));
        //直流功率

        //发电量
        name1="ModBus.逆变器";
        name2=".当日发电量01kWh";
        gl1 = jsonObject.get("1w").toString();
        gl2 = jsonObject.get("2w").toString();
        gl3 = jsonObject.get("3w").toString();
        gl=new BigDecimal(gl1).add(new BigDecimal(gl2)).add(new BigDecimal(gl3));
        this.add(energyCode,null,name1+name2,gl);
        //发电量

        //温度
        name1="ModBus.室外温湿度传感器";
        name2=".温度";
        gl1 = jsonObject.get("1t1").toString();
        gl2 = jsonObject.get("2t1").toString();
        gl3 = jsonObject.get("3t1").toString();
        gl=new BigDecimal(gl1).divide(new BigDecimal(10));
        this.add(energyCode,"B1N02-D01(B栋)",name1+"1"+name2,gl);
        gl=new BigDecimal(gl2).divide(new BigDecimal(10));
        this.add(energyCode,"B1N02-D01(A栋)",name1+"2"+name2,gl);
        gl=new BigDecimal(gl3).divide(new BigDecimal(10));
        this.add(energyCode,"B1N01-D01(A栋)新",name1+"3"+name2,gl);
        //温度
    }
    private void add(String energyCode,String macCode,String name,BigDecimal gl){
        if (gl.compareTo(new BigDecimal(0))==-1){
            return;
        }
//        if (gl.compareTo(new BigDecimal(1000000))==1){
//            return;
//        }
        try{
            NengKongData glData=new NengKongData();
            glData.setName(name);
            glData.setValue(gl);
            glData.setProduceTime(DateTimeUtil.dqsj());
            glData.setEnergyCode(energyCode);
//            glData.setDId(System.currentTimeMillis());
            glData.setMacCode(macCode);
            glData.setHourTime(DateTimeUtil.dqsj());
            TimerUtil.list.add(glData);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public BigDecimal pdsj1(BigDecimal a){
        Integer hour = DateTimeUtil.getHour();
        if (hour>20 || hour<6){
            if (a.compareTo(glmrz1)==0){
                a=new BigDecimal(0);
            }else {
                glmrz1=a;
            }
        }
        return a;
    }
    public BigDecimal pdsj2(BigDecimal a){
        Integer hour = DateTimeUtil.getHour();
        if (hour>20 || hour<6){
            if (a.compareTo(glmrz2)==0){
                a=new BigDecimal(0);
            }else {
                glmrz2=a;
            }
        }
        return a;
    }
}
